package com.kai.service.ipml;

import com.github.pagehelper.PageHelper;
import com.kai.entity.OaAdminUser;
import com.kai.entity.User;
import com.kai.mapper.first.OaAdminUserMapper;
import com.kai.mapper.first.UserMapper;
import com.kai.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by kai on 18-8-1.
 */
@Service("UserService")
//@Transactional(rollbackFor={Exception.class})
public class UserServiceImpl implements UserService {

    private static Logger log = LoggerFactory.getLogger(UserServiceImpl.class);
    @Resource
    private UserMapper userMapper;
    @Resource
    private OaAdminUserMapper oaAdminUserMapper;


    @Override
    public Map<String, Object> getTableData(Map<String, Object> map) {
        Map<String, Object> tableData = new HashMap<>();
        try {

            int pageNum=Integer.parseInt(map.get("pageNum").toString());
            int pageSize=Integer.parseInt(map.get("pageSize").toString());
            PageHelper.startPage(pageNum, pageSize);

            PageHelper.startPage(pageNum, pageSize);
            List<User> userList = userMapper.findUsersByUsername(map);
            int count = userMapper.getCount(map);
            tableData.put("list", userList);
            tableData.put("count", count);

            return tableData;
        } catch (Exception e) {
            log.info("获取列表失败");
            e.printStackTrace();

        }

        return tableData;
    }

    @Override
    public List<OaAdminUser> getUser(OaAdminUser user) {
        String userName = user.getUsername();
        String password = user.getPassword();
        List<OaAdminUser> users=new ArrayList<>();
        OaAdminUser ao=new OaAdminUser();
        try {
            if (userName != null) {
                ao.setUsername(userName);
            }
            users = oaAdminUserMapper.findUsersByUsername(ao);
            return users;
        } catch (Exception e) {
            log.info("获取用户信息失败");
            e.printStackTrace();
            return users;
        }

    }


    @Override
    public Map<String, Object> getListpage(Map<String, Object> map) {

        Map<String, Object> tableData = new HashMap<>();
        try {
            int pageNum=Integer.parseInt(map.get("pageNum").toString());
            int pageSize=Integer.parseInt(map.get("pageSize").toString());
            PageHelper.startPage(pageNum, pageSize);
            List<User> userList = userMapper.findUsersByUsername(map);
            int count = userMapper.getCount(map);

            tableData.put("list", userList);
            tableData.put("count", count);
            return tableData;
        } catch (Exception e) {
            log.info("获取列表失败");
            e.printStackTrace();
        }
        return tableData;
    }
}
